package ${domain}.frame.auth;

import ${domain}.module.wsys.ent.Res;
import ${domain}.module.wsys.mgr.ResManager;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Token - 通行证类
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
public class Token implements Serializable {
    private static final Long serialVersionUID = 1L;
    /**
     * ID
     */
    private long id;
    /**
     * TOKEN
     */
    private String token;
    /**
     * 用户ID
     */
    private long userId;
    /**
     * 用户名称
     */
    private String userName;
    /**
     * 用户别名
     */
    private String userAlias;

    private Set<String> resSet = new HashSet<>();

    public boolean hasRes(String res) {
        {// todo 开发初期收集资源，后期删除
            String active = LocalData.getActive();
            if (active.contains("dev")) {//测试环境捕获资源
                ResManager resManager = LocalData.getBean(ResManager.class);
                resManager.saveRes(res, LocalData.getSysToken());
            }
        }

        for (String s : resSet) {
            if (res.matches(s)) {
                return true;
            }
        }

        return false;
    }

    public void putRes(String resource) {
        resSet.add(resource);
    }

    public void putRes(Set<String> resourceSet) {
        this.resSet.addAll(resourceSet);
    }

    public void putRes(List<Res> resList) {
        if (resList == null) return;
        for (Res res : resList) {
            this.resSet.add(res.getResValue());
        }
    }

    public Set<String> getResSet() {
        return resSet;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserAlias() {
        return userAlias;
    }

    public void setUserAlias(String userAlias) {
        this.userAlias = userAlias;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}
